package com.zzyl.serve.controller;

import java.util.List;
import java.util.Map;

import com.zzyl.common.core.domain.R;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.vo.DeviceDetailVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author itheima
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 从物联网平台同步产品列表 到 redis
     *
     * /nursing/device/syncProductList
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表 到 redis")
    public AjaxResult syncProductList()
    {

        deviceService.syncProductList();

        return AjaxResult.success();
    }



    /**
     * 查询产品列表:
     *
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询产品列表")
    public AjaxResult allProduct()
    {

        List<ProductVo> voList = deviceService.allProduct();

        return AjaxResult.success(voList);
    }


    /**
     *
     * 注册设备
     * 接口地址:/nursing/device/register
     * 请求方式:POST
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult register(@RequestBody  DeviceDto deviceDto) {

        deviceService.registerDevice(deviceDto);

        return  success();
    }




    /**
     *
     * 设备分页查询
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }



    /**
     *
     * 设备详情查询
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "获取设备列表")
    public R<DeviceDetailVo> findDeviceDetailById(@PathVariable String iotId)
    {

        DeviceDetailVo deviceDetailVo = deviceService.findDeviceDetailById(iotId);


        return R.ok(deviceDetailVo);
    }





    /**
     *
     * 查看设备最近上报数据
     *
     * 接口地址: /nursing/device/queryServiceProperties/{iotId}
     * 请求方式:GET
     * 请求示例:
     * 路径参数：iotId  设备id
     *
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查看设备最近上报数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId)
    {

        List<Map> list = deviceService.queryServiceProperties(iotId);


        return success(list);
    }


    /**
     * 查询产品详情(报警规则模块使用)
     * 接口地址:/nursing/device/queryProduct/{productKey}
     * 请求方式:GET
     * 请求示例:
     * 路径参数：productKey   产品的id
     */

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情(报警规则模块使用)")
    public AjaxResult queryProduct(@PathVariable String productKey)
    {

        AjaxResult result =  deviceService.queryProduct(productKey);


        return result;
    }



}
